package com.burgeon.backend.SHOPBACKEND.rest.marketing;

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class QueryMarketingVipVO extends RESTBase {
    private String ip;
    private String account;
    private String password;
    private final static int r = 20;
    public QueryMarketingVipVO(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public HashMap getData(String id,String[] arr,int start) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(id,arr,start), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        HashMap hdata = new HashMap();
        ArrayList<HashMap> dataList = new ArrayList<HashMap>();
        for (int i=0;i<jsonArray.length();i++){
            JSONArray data = jsonArray.getJSONArray(i);
            HashMap hashMap = new HashMap();
            hashMap.put("id",data.getString(0));
            hashMap.put("vip",data.getString(1));
            hashMap.put("price",data.getString(2));
            hashMap.put("integral",data.getString(3));
            hashMap.put("attention","null".equals(data.getString(4))?"未关注":"已关注");
            hashMap.put("store",data.getString(5));
            hashMap.put("emp","null".equals(data.getString(6))?"无":data.getString(6));
            dataList.add(hashMap);
        }
        hdata.put("data",dataList);
        hdata.put("count",count);

        return hdata;
    }

    private MultiValueMap Data(String id,String[] arr,int start) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("CARDNO");
        columns.add("'FA_VIPACC_ID;TOT_AMT_ACTUAL'");
        columns.add("'INTEGRAL'");
        columns.add("OPENID");
        columns.add("'C_STORE_ID;NAME'");
        columns.add("'HR_EMPLOYEE_ID;NAME'");

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        params.put("count","true");
        params.put("range",r);
        params.put("start",String.valueOf(start*r));
        HashMap paramss = new HashMap();
        paramss.put("column", "'YC_"+id+"LABELDEFINE_ID'");
        paramss.put("condition","\" IN ("+ toString(arr)+")\"");
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }
    private String toString(String[] data){

        return Arrays.toString(data).replace("\"","'")
                .replace("[","")
                .replace("]","")
                .replace(" ", "");

    }

    public static void main(String[] args) {

        String[] arr = new String[]{"163","2"};
        QueryMarketingVipVO queryMail = new QueryMarketingVipVO("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(queryMail.getData("1",arr,0));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
